#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;  // 示例：元素类型为int，可根据需求修改
#define LIST_INIT_SIZE 50  // 初始容量
#define LIST_INCREMENT 10   // 扩容增量

typedef struct
{
   ElemType *elem;   // 指向元素存储区的指针
   int length;
   int listsize;
}SqList;

// 1. 初始化顺序表：分配初始内存，初始化长度和容量
int InitList_Sq(SqList *L);

int ListInsert_Sq(SqList *L,ElemType e);

int ListDelete_Sq(SqList *L,int val,ElemType *e);

int IncreaseListSize_Sq(SqList *L);

void printflist(SqList L);
int ListDeleteDuplicates_Sq(SqList *L,ElemType base);

int main()
{
    SqList List;
    ElemType e;
    InitList_Sq(&List);
    List.elem[0] = 2;
    List.elem[1] = 2;
    List.elem[2] = 3;
    List.elem[3] = 3;
    List.elem[4] = 3;
    List.length = 5;
   // ListInsert_Sq(&List,4);
    printflist(List);
    ListDeleteDuplicates_Sq(&List,2);
    printflist(List);
    ListDeleteDuplicates_Sq(&List,3);
    printflist(List);

    return 0;  
}


int InitList_Sq(SqList *L)
{
    L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if (L->elem==NULL)
    {
        printf("内存分配失败！");
        return -1;
    }
    L->length=0;
    L->listsize=LIST_INIT_SIZE;
    return 1;
}

int ListInsert_Sq(SqList *L,ElemType e)
{
    if (L==NULL)
    {
        printf("错误！");
        return -1;
    }

    ElemType *q;
    int pos=0;

    while (pos<L->length && L->elem[pos]<=e)
    {
        pos++;
    }

    if (L->length >= L->listsize) {
        if (IncreaseListSize_Sq(L) != 1) 
        { 
            return -1;
        }
    }

   for (int i = L->length - 1; i >= pos; i--) 
    {
        L->elem[i + 1] = L->elem[i];
    }

   L->elem[pos] = e;
   L->length++;
    return 1;
}

int ListDelete_Sq(SqList *L,int val,ElemType *e)
{
    if (L==NULL||e==NULL)
    {
        printf("错误！");
        return -1;
    }
    int deleted = 0;  
    for (int i = 0; i < L->length;)
    {
       if (L->elem[i]==val)
       {
        *e=L->elem[i];
        deleted = 1;
       for (int j = i; j < L->length - 1; j++) 
        {
            L->elem[j] = L->elem[j + 1];
        }
        --L->length;
       }
       else
       {
        i++;
       }  
    }
   return deleted ? 1 : 0;
}

int IncreaseListSize_Sq(SqList *L) 
{
    if (L == NULL) 
    {
        return -1; 
    }

    ElemType *old_elem = L->elem;
    L->elem = (ElemType *)realloc(old_elem,(L->listsize + LIST_INCREMENT) * sizeof(ElemType) );

    if (L->elem == NULL)
    {
        L->elem = old_elem;  
        return -1;     
    }
    L->listsize += LIST_INCREMENT;

    return 1;  
}

void printflist(SqList L)
{
    for (int i = 0; i < L.length; i++)
    {
       printf("%d\t",L.elem[i]);
    }
    printf("\n");
}

int ListDeleteDuplicates_Sq(SqList *L, ElemType base) {
    if (L == NULL || L->length == 0) {
        return (L == NULL) ? -1 : 1;  // 空表或无效指针，返回对应状态
    }

    // 1. 找到base第一次出现的位置first_pos
    int first_pos = -1;
    for (int i = 0; i < L->length; i++) {
        if (L->elem[i] == base) {
            first_pos = i;
            break;  // 只保留第一个出现的位置
        }
    }

    // 若base不存在，无需删除
    if (first_pos == -1) {
        printf("未找到元素%d，无需删除\n", base);
        return 1;
    }

    // 2. 从first_pos + 1开始，手动删除后续所有base（不删除第一个）
    for (int i = first_pos + 1; i < L->length;) {  // i不固定递增，避免漏删
        if (L->elem[i] == base) {
            // 移动后续元素覆盖当前base（删除当前base）
            for (int j = i; j < L->length - 1; j++) {
                L->elem[j] = L->elem[j + 1];
            }
            L->length--;  // 长度减1
        } else {
            i++;  // 不是base，继续遍历下一个
        }
    }

    return 1;
}

